﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditVisaPrice : AdminPageBase
    {
        private VisaPriceInfo _visaPriceInfo = new VisaPriceInfo();
        private readonly VisaPriceController _visaPriceController = new VisaPriceController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["VisaPriceId"]))
                _visaPriceInfo = _visaPriceController.GetVisaPrice(int.Parse(Request.QueryString["VisaPriceId"]));

            if (Page.IsPostBack) return;

            LoadNational();
            LoadStatus();
            LoadUser();
            LoadRegisterNational();
            LoadVisaType();
            LoadVisa();
           
            if (_visaPriceInfo.VisaPriceId == -1) return;

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListVisaPrice.aspx?xml=VisaPrice");
        }

        private void SaveData()
        {
            _visaPriceInfo.VisaTypeId = int.Parse(ddlVisaType.SelectedValue);
            _visaPriceInfo.VisaId = int.Parse(ddlVisa.SelectedValue);
            _visaPriceInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _visaPriceInfo.NationalId = int.Parse(ddlNational.SelectedValue);
            _visaPriceInfo.RegisterNational = int.Parse(ddlRegisterNational.SelectedValue);
            _visaPriceInfo.Price = decimal.Parse(txtPrice.Text);
            _visaPriceInfo.Stranlate = decimal.Parse(txtStranlate.Text);
            _visaPriceInfo.OtherCosts= decimal.Parse(txtOtherCosts.Text);
            _visaPriceInfo.VisaQuantity = int.Parse(txtVisaQuantity.Text);
            _visaPriceInfo.TotalPrice = int.Parse(txtStranlate.Text) + int.Parse(txtPrice.Text) +
                                         int.Parse(txtOtherCosts.Text);

            if (_visaPriceInfo.VisaPriceId == -1)
            {
                _visaPriceInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _visaPriceInfo.CreatedDate = DateTime.Now;
                _visaPriceController.InsertVisaPrice(_visaPriceInfo);
            }
            else
            {
                _visaPriceInfo.UpdatedBy = UserId;
                _visaPriceInfo.UpdatedDate = DateTime.Now;
                _visaPriceController.UpdateVisaPrice(_visaPriceInfo);
            }

            Response.Redirect("/Pages/ListVisaPrice.aspx?xml=VisaPrice");
        }

        private void RenderForm()
        {
            ddlVisaType.SelectedValue = _visaPriceInfo.VisaTypeId.ToString();
            ddlVisa.SelectedValue = _visaPriceInfo.VisaId.ToString();
            ddlNational.SelectedValue = _visaPriceInfo.NationalId.ToString();
            ddlRegisterNational.SelectedValue = _visaPriceInfo.RegisterNational.ToString();
            ddlStatus.SelectedValue = _visaPriceInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _visaPriceInfo.CreatedBy.ToString();
            txtPrice.Text = _visaPriceInfo.Price.ToString();
            txtStranlate.Text = _visaPriceInfo.Stranlate.ToString();
            txtOtherCosts.Text = _visaPriceInfo.OtherCosts.ToString();
            txtVisaQuantity.Text = _visaPriceInfo.VisaQuantity.ToString();
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNational().OrderBy(n => n.NationalName);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
        private void LoadRegisterNational()
        {
            ddlRegisterNational.DataSource = new NationalController().ListNational().OrderBy(n => n.NationalName);
            ddlRegisterNational.DataTextField = "NationalName";
            ddlRegisterNational.DataValueField = "NationalId";
            ddlRegisterNational.DataBind();

            ddlRegisterNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
       
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadVisaType()
        {
            ddlVisaType.DataSource = new VisaTypeController().ListVisaType().OrderBy(v=>v.VisaTypeName);
            ddlVisaType.DataTextField = "VisaTypeName";
            ddlVisaType.DataValueField = "VisaTypeId";
            ddlVisaType.DataBind();

            ddlVisaType.Items.Insert(0, new ListItem("--- Chọn loại Visa ---", "-1"));
        }
        private void LoadVisa()
        {
            //ddlVisa.DataSource = new VisaController().ListVisaByVisaType(_visaPriceInfo.VisaTypeId);
            ddlVisa.DataSource =
               new VisaController().ListVisa()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.VisaName);
            ddlVisa.DataTextField = "VisaName";
            ddlVisa.DataValueField = "VisaId";
            ddlVisa.DataBind();

            ddlVisa.Items.Insert(0, new ListItem("--- Chọn Visa ---", "-1"));
        }
    }
}